// pages/course/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden:false,
    curNav: 1,
    curIndex: 0,
    cart: [],
    cartTotal: 0,
    dishes: [],
    navList: [{
      id: 1,
      name: '学习课程',
      method:'selectCourseStudy'
    },
    {
      id: 2,
      name: '课后练习',
      method: 'selectCoursExercise'
    }],
    dishesList: [],
    server: app.data.server
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = app.globalData.userInfo;
    if (userInfo==null){
      wx.navigateTo({
        url: '../login/index',
      });
      return;
    }
    
    this.loadingChange()
    var dishesList = this.data.dishesList;
    var _this=this;
    var userId = userInfo.id;
    var time = new Date();
    debugger
    app.paiRequest("/app/course/study/tcourseStudy/selectCourseStudy?time="+time, { "userId": userInfo }).then(function (res) {
      var studyList=res.data;
      dishesList[0] = studyList;
      _this.setData({
        dishesList: dishesList
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadingChange:function(){
    setTimeout(() => {
      this.setData({
        hidden:true
      })
    },2000)
  },
  selectNav:function(event){
       let id=event.target.dataset.id,index=parseInt(event.target.dataset.index),method=event.target.dataset.method;
       self=this;
       this.setData({
         curNav:id,
         curIndex:index
       })
    var dishesList = this.data.dishesList;
    var _this = this;
    var time = new Date();
    app.paiRequest("/app/course/study/tcourseStudy/selectCourseStudy?time="+time , { item: 1 }).then(function (res) {
      console.log(res);
      var studyList = res.data;
      dishesList[index] = studyList;
      _this.setData({
        dishesList: dishesList
      })
    });
  },
  setStatus(dishId) {
    let dishes = this.data.dishesList;
    for (let dish of dishes) {
      dish.forEach((item) => {
        if (item.id == dishId) {
          item.status = !item.status || false
        }
      })
    }

    this.setData({
      dishesList: this.data.dishesList
    })
  },
  selectDish: function (event) {
    let id = event.currentTarget.dataset.dish;
    let src = event.currentTarget.dataset.src;
    wx.navigateTo({
      url: '/pages/' + src + '?id=' + id,
    })
  },
  selectDish2:function(event){
    let id = event.currentTarget.dataset.dish;
    let src = event.currentTarget.dataset.src;
    wx.navigateTo({
      url:src + '?id=' + id,
    })
  }
})